<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>题解 P2426 【删数】 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="DP大法好！">
<meta property="og:type" content="article">
<meta property="og:title" content="题解 P2426 【删数】">
<meta property="og:url" content="http://example.com/2020/08/24/%E9%A2%98%E8%A7%A3%20P2426%20%E3%80%90%E5%88%A0%E6%95%B0%E3%80%91/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="DP大法好！">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-24T11:21:11.720Z">
<meta property="article:modified_time" content="2023-07-19T11:50:54.994Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-题解 P2426 【删数】" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/08/24/%E9%A2%98%E8%A7%A3%20P2426%20%E3%80%90%E5%88%A0%E6%95%B0%E3%80%91/" class="article-date">
  <time class="dt-published" datetime="2020-08-24T11:21:11.720Z" itemprop="datePublished">2020-08-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      题解 P2426 【删数】
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>DP大法好！</p>
<p><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P2426">洛谷题目传送门</a></p>
<p><del>一眼看去：区间DP</del></p>
<p><del>数据范围：三重循环</del></p>
<p>好了不装B了，开始说正事</p>
<p>这题<del>非常明显</del>是区间DP。</p>
<p>按照惯例，先定义状态。</p>
<p>分析题目，发现除了区间左端点和右端点之外，什么也不需要加进状态里。因为<del>显而易见</del>除了区间左右端点，没有什么能够影响答案。</p>
<p>所以我们定义状态$dp[l][r]$为区间$[l,r]$的最大答案。</p>
<p>这个“操作价值”可以两重循环预处理出来，所以用$pre[l][r]$代表删除区间$[l,r]$的最大价值。非常明显的，<del>甚至题目里已经直接写明白了，</del> <del>其实不用预处理，现场算就行，反正是$O(1)$的</del></p>
<script type="math/tex; mode=display">
pre[l][r]=
\begin{cases} a[l],
& \text {$l=r$} \\ 
|a[l]-a[r]|\times (r-l+1),  & \text{$else$} \end{cases}</script><p>然后就是最重要的一步——状态转移方程。</p>
<p>题目里有一个操作，就是一个区间删除一些数，失去一些“潜在的”价值。那么把这个过程反过来，一个区间加上一些数，得到一些“潜在的”价值。答案就是区间$[1,n]$的最大潜在价值。</p>
<p>可以得到：</p>
<script type="math/tex; mode=display">
dp[l][r]=\max_{i⊆[l,r-1]} \max (dp[l][i]+pre[i+1][r],pre[l][i]+dp[i+1][r])</script><p>翻译成人话就是，一个区间的潜在价值，等于从左边删去一些数或者从右边删去一些数后再加上删去的数的价值的较大值。</p>
<p>知道了状态转移方程，代码就非常好写了。</p>
<p>AC Code:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span> <span class="comment">//赞美万能头！</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 105</span></span><br><span class="line"><span class="type">int</span> n,a[MAXN],dp[MAXN][MAXN],pre[MAXN][MAXN];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">        pre[i][i]=dp[i][i]=a[i];<span class="comment">//因为题目中的特殊约定“如果只去掉一个数，操作价值为这个数的值。”</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> len=<span class="number">2</span>;len&lt;=n;len++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> l=<span class="number">1</span>;l&lt;=n-len+<span class="number">1</span>;l++)&#123;</span><br><span class="line">            <span class="type">int</span> r=l+len<span class="number">-1</span>;</span><br><span class="line">            pre[l][r]=dp[l][r]=<span class="built_in">abs</span>(a[l]-a[r])*(r-l+<span class="number">1</span>);<span class="comment">//预处理，因为有可能最优方案是把区间[l,r]都删掉，所以要给dp[l][r]也赋值。</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> len=<span class="number">2</span>;len&lt;=n;len++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> l=<span class="number">1</span>;l&lt;=n-len+<span class="number">1</span>;l++)&#123;</span><br><span class="line">            <span class="type">int</span> r=l+len<span class="number">-1</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i=l;i&lt;=r<span class="number">-1</span>;i++)&#123;</span><br><span class="line">                dp[l][r]=<span class="built_in">max</span>(dp[l][r],dp[l][i]+pre[i+<span class="number">1</span>][r]);</span><br><span class="line">                dp[l][r]=<span class="built_in">max</span>(dp[l][r],pre[l][i]+dp[i+<span class="number">1</span>][r]);<span class="comment">//就是状态转移方程233</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,dp[<span class="number">1</span>][n]);<span class="comment">//输出总的“潜在价值”。</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>由于LZ非常菜，有可能有自以为是的地方，所以请在评论区无情的指出qwq</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/08/24/%E9%A2%98%E8%A7%A3%20P2426%20%E3%80%90%E5%88%A0%E6%95%B0%E3%80%91/" data-id="clk9o8lna000m8wi59j5d6yz8" data-title="题解 P2426 【删数】" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/08/26/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E8%AF%A6%E8%A7%A3%20AND%E3%80%8CP1433%E3%80%8D%E9%A2%98%E8%A7%A3/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          模拟退火详解 AND P1433题解
        
      </div>
    </a>
  
  
    <a href="/2020/08/06/SPFA%E7%AE%97%E6%B3%95%E8%AF%A6%E8%A7%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">SPFA算法详解</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>